package com.talent.web.config.security.phone;

import com.talent.web.enums.ResponseEnum;
import com.talent.web.exception.TalentException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName：SmsCodeServiceImpl
 * @Author: hszhou
 * @Date: 2025/2/28 14:11
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Service
@RequiredArgsConstructor
public class SmsCodeServiceImpl implements SmsCodeService{

    private final RedisTemplate<String,Object> redisTemplate;
    @Override
    public String generateCode(String phone) {
        String code ="123";
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();

        Object o = valueOperations.get(phone);
        if(o!=null){
            throw new TalentException(ResponseEnum.CAPTCHA_EXIT);
        }
        valueOperations.set(phone,code, 60, TimeUnit.SECONDS);
        return code;
    }

    @Override
    public boolean validateCode(String phone, String code) {

        Object o = redisTemplate.opsForValue().get(phone);

        return o!=null&& o.toString().equals(code);

    }
}
